<?php
/* explain:		logs class
 * Project:     marsems
 * File:        Logs.php
 *
 * @ link 		http://www.../lib/
 * @ Email		ldmmyx@hotmail.com
 * @ copyright 	2007  Ling Deming	
 * @ author 	Ivan  Ling
 * @ version 3.0
 *
 */
include_once("impl/LogImpl.php");
include_once("domain/LoginLogDomain.php");
include_once("domain/LoginBlockLogDomain.php");

class LoginLogImpl extends LogImpl
{
	const STATUS_PAUSE 	= "pause";
	const STATUS_NORMAL = "normal";
	const STATUS_BLACK 	= "black";
	
	
	function __construct($DBTag)
	{
		parent::__construct($DBTag);
	}
	
	public function addLoginLog($obj)
	{		
		if($obj instanceof LoginLogDomain){
			$sql 	 = "INSERT INTO login_log SET userId=";
			$sql	.= $obj->getUserId();
						
			$sql 	.= ", loginTime='";
			$sql	.= $obj->getLoginTime();
			
			$sql 	.= "', logoutTime='";
			$sql	.= $obj->getLogoutTime();
	
			$sql 	.= "', ip='";
			$sql	.= $obj->getIp();
	
			$sql	.= "' ON DUPLICATE KEY UPDATE ip=Values(ip)";
			
			$id = $this->insert($this->DBTag, $sql);
			
			$obj->setId($id);
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of LoginLogDomain.';
		}
		
		return $id;
	}
	
	public function logout($id)
	{		
		$logoutTime = date("Y-m-d H:i:s");
		
		$sql = "UPDATE login_log SET logoutTime = '$logoutTime' WHERE id = $id";
		
		return $this->update($this->DBTag, $sql);
	}
	
	public function getLoginLog($id)
	{		
		$sql 	= "SELECT userId, loginTime, logoutTime, ip FROM login_log WHERE id = $id";
		
		$obj 	= null;
		$conn 	= null;
		
		try {
			$conn = $this->getConnection($this->DBTag);
			
			$rs = mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$obj = new LoginLogDomain();

				$obj->setUserId($row[0]);
				$obj->setLoginTime($row[1]);				
				$obj->setLogoutTime($row[2]);
				$obj->setIp($row[3]);
				$obj->setId($id);
			} else {
				$this->message = "get data failed by Id. sql : $sql <br>";
				$this->message .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
			
		return $obj;
	}
	
	
	/* add login block log
	 *
	 * @ param  return boolean login block object
	 */
	public function addLoginBlockLog($obj)
	{		
		if($obj instanceof LoginBlockLogDomain){
			$sql 	 = "INSERT INTO login_block_log SET userId=";
			$sql	.= $obj->getUserId();
						
			$sql 	.= ", loginTime='";
			$sql	.= $obj->getLoginTime();
			
			$sql 	.= "', loginCount=";
			$sql	.= $obj->getLoginCount();
	
			$sql 	.= ", ip='";
			$sql	.= $obj->getIp();
		
			$sql 	.= "', action='";
			$sql	.= $obj->getAction();
	
			$sql 	.= "', blockTime='";
			$sql	.= $obj->getBlockTime();
	
			$sql	.= "' ON DUPLICATE KEY UPDATE ip=Values(ip), loginTime=Values(loginTime), action=Values(action), blockTime=Values(blockTime), loginCount = loginCount + 1";
			
			$id = $this->insert($this->DBTag, $sql);			
		}else{
			$id	= -1;
			
			$this->message = 'input object is not instance of LoginBlockLogDomain.';
		}
		
		return $id;
	}
	
	public function getLoginBlockLog($userId)
	{		
		$sql 	= "SELECT loginTime, loginCount, ip, action, blockTime FROM login_block_log WHERE userId = $userId";
		
		$obj 	= null;
		$conn 	= null;
		
		try {
			$conn = $this->getConnection($this->DBTag);
			
			$rs = mysql_query($sql, $conn);
			if ($row = mysql_fetch_row($rs)) {
				$obj = new LoginBlockLogDomain();

				$obj->setUserId($userId);
				$obj->setLoginTime($row[0]);				
				$obj->setLoginCount($row[1]);
				$obj->setIp($row[2]);
				$obj->setAction($row[3]);
				$obj->setBlockTime($row[4]);
			} else {
				$this->message = "get data failed by Id. sql : $sql <br>";
				$this->message .= "error: " . mysql_error($conn);
			}
		} catch (Exception $e) {
			$this->message = $e->getMessage();
		}
		
		$this->freeConnection($this->DBTag, $conn);
			
		return $obj;
	}
	
	

}
?>
